package com.kili.picture.controller;


import com.kili.picture.context.Result;
import com.kili.picture.context.ResultStatus;
import com.kili.picture.service.PictureService;
import com.kili.picture.tools.WebAsyncTaskHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 图片 前端控制器
 * </p>
 *
 * @author Jar
 * @since 2019-04-01
 */
@RestController
@Slf4j
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private WebAsyncTaskHelper webAsyncTaskHelper;

    @PostMapping("/upload")
    public WebAsyncTask<Result> upload(MultipartFile file, String module) {
        Assert.notNull(file, "File must not be null");
        Assert.hasText(file.getOriginalFilename(), "File Name is empty");
        return webAsyncTaskHelper.asyncCall(
                () -> {
                    String upload = pictureService.upload(file, module);
                    log.info("Successful picture upload - fileName={}", file.getOriginalFilename());
                    return Result.of(upload);
                }, () -> {
                    log.error("Picture upload failed - fileName={}", file.getOriginalFilename());
                    return Result.of(ResultStatus.INTERNAL_SERVER_ERROR);
                }, () -> {
                    log.error("Picture upload timeout - fileName={}", file.getOriginalFilename());
                    return Result.of(ResultStatus.REQUEST_TIMEOUT);
                });
    }

    @PostMapping("/upload/tmp")
    public WebAsyncTask<Result> uploadToTemp(MultipartFile file, String module) {
        Assert.notNull(file, "File must not be null");

        return webAsyncTaskHelper.asyncCall(
                () -> {
                    String upload = pictureService.uploadToTmp(file, module);
                    return Result.of(upload);
                }, () -> {
                    log.error("UploadToTemp failed - fileName={}", file.getOriginalFilename());
                    return Result.of(ResultStatus.INTERNAL_SERVER_ERROR);
                }, () -> {
                    log.error("UploadToTemp timeout - fileName={}", file.getOriginalFilename());
                    return Result.of(ResultStatus.REQUEST_TIMEOUT);
                });
    }

    @PostMapping("/tmp/save")
    public Result tempSave(@RequestBody String[] paths) {
        Assert.notNull(paths, "Paths must not be null");
        return Result.of(pictureService.tmpToOSS(paths));
    }

    @DeleteMapping("/delete")
    public Result delete(String path) {
        Assert.notNull(path, "Path must not be null");
        return Result.of(pictureService.delete(path));


    }

}
